package com.example.lib_car.applyparking

import com.example.lib_car.ApiService
import com.example.lib_common.mvi.BaseRepository
import com.example.lib_common.mvi.ResponseEntity
import com.example.network_sdk.HttpType
import com.example.network_sdk.NetWorkFactory
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ApplyParkingRepository : BaseRepository() {
    fun requestSubParking(map: HashMap<String,Any>) : Flow<ResponseEntity<CarEntity>> {
        return NetWorkFactory.factory(HttpType.TOKEN)!!
            .create(ApiService::class.java)
            .requestCommitParking(createBody(map));
    }

    fun requestColorCar() : Flow<ResponseEntity<List<ColorEntity>>> {
        return NetWorkFactory.factory(HttpType.TOKEN)!!
            .create(ApiService::class.java)
            .requestCarColor();
    }

    fun requestBelong(map: HashMap<String, Any>) : Flow<ResponseEntity<List<DepartmentEntity>>> {
        return NetWorkFactory.factory(HttpType.TOKEN)!!
            .create(ApiService::class.java)
            .requestBelong(createBody(map));
    }

    fun requestAddCar(map: HashMap<String, Any>) : Flow<ResponseEntity<AddCarEntity>> {
        return NetWorkFactory.factory(HttpType.TOKEN)!!
            .create(ApiService::class.java)
            .requestAddCar(createBody(map));
    }

    fun upLoadImg(path : String) : Flow<ResponseEntity<UpLoadImgEntity>>{
        val builder = MultipartBody.Builder()
        val body = RequestBody.create(MediaType.parse("multipart/form-data"), File(path))
        builder.addFormDataPart("file", path.substring(path.lastIndexOf("/")), body)
        return NetWorkFactory.factory(HttpType.NONE)!!
            .create(ApiService::class.java)
            .uploadIms(builder.build().parts())
    }
}